package com.llkj.qingnianguoxue.util;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.llkj.qingnianguoxue.bean.response.ResponseData;
import com.llkj.qingnianguoxue.bean.response.ResponseList;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

public class GsonUtil {

	private static Gson gson = null;
	static {
		if (gson == null) {
			gson = new Gson();
		}
	}

	private GsonUtil() {
	}

	/**
	 * 转成json
	 *
	 * @param object
	 * @return
	 */
	public static String GsonString(Object object) {
		String gsonString = null;
		if (gson != null) {
			gsonString = gson.toJson(object);
		}
		return gsonString;
	}

	/**
	 * 转成bean
	 *
	 * @param gsonString
	 * @param cls
	 * @return
	 */
	public static <T> T GsonToBean(String gsonString, Class<T> cls) {
		T t = null;
		if (gson != null) {
			t = gson.fromJson(gsonString, cls);
		}
		return t;
	}
	/**
	 * 转成带list的bean
	 * @param gsonString
	 * @param cls
	 * @return
	 */

	public static <T> T GsonToListBean(String gsonString, Class<T> cls) {
		T t = null;
		if (gson != null) {
			t = gson.fromJson(gsonString, cls);
		}
		return t;
	}
	/**
	 * 转成list
	 *
	 * @param gsonString
	 * @param cls
	 * @return
	 */
	public static <T> List<T> GsonToList(String gsonString, Class<T> cls) {
		List<T> list = null;
		if (gson != null) {
			list = gson.fromJson(gsonString, new TypeToken<List<T>>() {
			}.getType());
		}
		return list;
	}

	/**
	 * 转成list中有map的
	 *
	 * @param gsonString
	 * @return
	 */
	public static <T> List<Map<String, T>> GsonToListMaps(String gsonString) {
		List<Map<String, T>> list = null;
		if (gson != null) {
			list = gson.fromJson(gsonString,
					new TypeToken<List<Map<String, T>>>() {
					}.getType());
		}
		return list;
	}

	/**
	 * 转成map的
	 * 
	 * @param gsonString
	 * @return
	 */
	public static <T> Map<String, T> GsonToMaps(String gsonString) {
		Map<String, T> map = null;
		if (gson != null) {
			map = gson.fromJson(gsonString, new TypeToken<Map<String, T>>() {
			}.getType());
		}
		return map;
	}

    /**
     * 处理{"id":id,"data":{}}这种结构数据，ResponseData最外层结构
     * @param json
     * 要解析的数据
     * @param clazz
     * data对应的Bean类
     * @return
     * ResponseData<T>对象
     */
    public static ResponseData fromJson(String json, Class clazz) {
        Gson gson = new Gson();
        Type objectType = type(ResponseData.class, clazz);
        return gson.fromJson(json, objectType);
    }
	/**
	 * 处理{"id":id,"data":[{},{}]}这种结构数据，ResponseList最外层结构；
	 * 例如ResponseList<ListBean> responseBean  = GsonUtil.fromJson(data,ListBean.class);
	 * List<ListBean> list = responseBean.getData();
	 * @param json
	 * 要解析的数据
	 * @param clazz
	 * data对应的Bean类
	 * @return
	 * ResponseList<T>对象
	 */
	public static ResponseList fromJsonList(String json, Class clazz) {
		Gson gson = new Gson();
		Type objectType = type(ResponseList.class, clazz);
		return gson.fromJson(json, objectType);
	}

    static ParameterizedType type(final Class raw, final Type... args) {
        return new ParameterizedType() {
            public Type getRawType() {
                return raw;
            }

            public Type[] getActualTypeArguments() {
                return args;
            }

            public Type getOwnerType() {
                return null;
            }
        };
    }
}